package xorg.tools.xml;

import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xorg.tools.util.FreemarkerUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 根据XML生成JAVA类
 * Created by iscdw on 2016/3/31.
 */
public class GenerateJavaFile {

    private static Logger logger = LoggerFactory.getLogger(GenerateJavaFile.class);
    private static List<String> generateElements = new ArrayList<String>();

    public Document load(String filename) {
        String path = GenerateJavaFile.class.getResource("/").getPath() + "/xml/" + filename;
        logger.info("加载XML,path={}", path);
        Document doc = null;
        Element root = null;
        List<Element> list = null;
        SAXReader reader = new SAXReader();

        try {
            return reader.read(new File(path));
        } catch (DocumentException e) {
            logger.error("读取XML出错,path={},err={}", path, e.getMessage());
            return null;
        }
    }

    public void generate(String packageName, Element root) {
        String className = StringUtils.capitalize(root.getName()); // 类名首字母大写
        String XmlRootElementName = ""; //XML节点名称
        logger.info("生成{}--beigin", className);
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("packageName", "com.test");
        map.put("className", className);
        map.put("xmlRootElement", className);

        List<Prop> list = new ArrayList<Prop>();

        List<Element> elements = root.elements();
        for (Element element : elements) {

            if (element.elements().size() > 0) {
                // 引用子对象
                Prop prop = new Prop();
                prop.setName(element.getName());
                prop.setType(StringUtils.capitalize(element.getName()));
                list.add(prop);

                generate(packageName, element);
            } else {
                // 引用属性
                Prop prop = new Prop();
                prop.setName(element.getName());
                prop.setType(element.attributeValue("_type") == null ? "String" : element.attributeValue("_type"));
                list.add(prop);
            }
        }
        map.put("list", list);
        try {
            if (generateElements.contains(className)) {
                logger.info("已生成{}跳过--end", className);
                return;
            } else {
                generateElements.add(className);

            }
            String content = FreemarkerUtils.fileFormat("xmlTemplate.ftl", map);

            System.out.println(content);
            logger.info("生成{}--end", className);
        } catch (Exception e) {
            logger.error("读取FreeMarker出错,filename=xmlTemplate.ftl", e);
        }

    }

    public void generateJavaFile(String packageName, String path) {

        Document doc = load(path);

        Element root = doc.getRootElement();

        Map<String, String> map = new HashMap<String, String>();

        map.put("packageName", "com.test");

        generate(packageName, root);


    }
}
